package com.github.rnewson.couchdb.lucene;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;  
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

public class Generation implements AbstractGeneration {

	private boolean stop = true;  //标志线程是否终止
	private String modification ="";
	private boolean busy = false; //标识线程是否繁忙
	Thread thread = null;
	String databasename = null;
	
	public void setThread(Thread thread)
	{
		this.thread = thread;
	}
	@Override
	public void run(){
		// TODO Auto-generated method stub

	 while(stop == false) //stop==true说明不再使用该线程，循环结束，线程结束
		{
			if(modification.length() != 0)
			{
				if(!interGenerateWeb(modification))
				{
					System.out.println("网站生成失败");
				}
				else
				{
					modification = ""; //网页生成成功，把以前的网页内容清空
					busy = false;      //网页生成成功，线程繁忙标志busy设置为false
				}
			}
		}
	}
	
	public void initGeneration(String databasename)//初始化网页生成线程，线程停止标志设为false，网页修改内容设为空，线程繁忙标志设为false
	{
		this.databasename = databasename;
		stop = false;
		modification = "";
		busy = false;
	}
	private boolean interGenerateWeb(String modification)//内部使用的网页生成函数，把网页内容写入文件中
	{
		System.out.println(modification);
		//////////////////////////////////////////////////////////////////////////////////// 
		//使用数据库和文档ID确定一个网页的名称
		String webname = null;
		
		try
		{
		final JSONObject json = new JSONObject(modification); 
		webname = json.getString("id");//从json对象中把id的内容读出来
		webname = this.databasename + "  id:" + webname;
		}
		catch(final Exception e)
		{
			System.err.println("json转换错误");
		}
		
		try
		{
		//先写日志文件再写网页
	    //写日志文件
		String filename = "log";
        FileOutputStream out = new FileOutputStream("/home/zzhj/log/" + filename, true); 
        //获得当前日期时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd   hh:mm:ss");     
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间           
        String date = formatter.format(curDate); 
        
        //把当前时期时间和修改的网页文件名写入日志文件
        PrintStream p=new PrintStream(out);
        String record = date + "  " +webname + "   content: " + modification + "/n";
        p.println(record);
        out.close();
        
        //写网页
        final JSONObject json2 = new JSONObject(modification); 
        String name = new String(this.databasename);
        name = name.replaceAll("/", "");
        name = name.replaceAll("http:", "");
        name = name.replaceAll(":", "_");
        out = new FileOutputStream("/home/zzhj/webs/" + name + "_" + json2.getString("id")); 
        p=new PrintStream(out);
        record = modification;
        p.println(record);
        out.close();
		}
		catch(final Exception e)
		{
			System.err.println("文件操作错误");
		}
        
//		try{
//			thread.sleep(100000);
//			}
//			catch (final Exception e) {
//                System.err.println("线程睡眠错误");
//			}
		return true;
	}
	
	public void stopThread(boolean stop)//终止本线程
	{
		this.stop = stop;
	}

	private void setBusy(boolean busy)
	{
		this.busy = busy;
	}
	
	public boolean getBusy()
	{
		return this.busy;
	}
	
	//外部使用的网页生成函数，把要生成的内容写入this.modification变量中。
	//run函数中会得到this.modification然后调用内部的网页生成函数执行实际
	//的文件写操作，把要生成的网页内容写入文件中。生成网页前先把繁忙标志设为true
	public void generateWeb(String modification)
	{
		this.modification =  modification;
		this.busy = true;
	}
}
